﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using static BankSimulation.ATM;

namespace BankSimulation
{
    internal class Account
    {
        public int AccountNumber { get; set; }
        public string Name { get; set; }
        public decimal Balance { get; set; }

        public Account(int accountNumber, string name, decimal balance)
        {
            AccountNumber = accountNumber;
            Name = name;
            Balance = balance;
        }
        public Account(){}
        public delegate void BigMoneyEventHandler(object sender, BigMoneyEventArgs e);

        public event EventHandler<BigMoneyEventArgs> BigMoneyWithdrawn;
        protected virtual void OnBigMoneyWithdrawn(BigMoneyEventArgs e)
        {
            BigMoneyWithdrawn?.Invoke(this, e);
        }

        public virtual void Withdraw(decimal amount)
        {
            if (amount >= 10000)
            {
                var args = new BigMoneyEventArgs(this, amount);
                OnBigMoneyWithdrawn(args);
            }
            if (amount > Balance)
            {
                throw new InsufficientFundsException();
            }
            Balance -= amount;
        }

        // 模拟坏钞
        private bool IsBadCash()
        {
            Random random = new Random();
            int randomNumber = random.Next(1, 101); // 生成1到100之间的随机数

            // 模拟坏钞率为30%
            return randomNumber <= 30;
        }

        public virtual void Depoist(decimal amount)
        {
            if (IsBadCash())
            {
                throw new BadCashException();
            }
            Balance += amount;

        }

        // 索引器，通过账号检索账户
        public Account this[int accountNumber]
        {
            get
            {
                if (this.AccountNumber == accountNumber)
                {
                    return this;
                }
                else
                {
                    return null; // 如果没有匹配的账户，返回null
                }
            }
        }
    }
}
